<template>
  <div class="relative w-full h-full">
    <PageHeader :title="$t('reason.原因管理')" />
    <SearchCom
      :searchList="searchList"
      @search="toSearch"
      @reset="handleReset"
    />
    <el-radio-group v-model="type" @change="handleCurrentChange(1)">
      <el-radio-button
        v-for="item in options"
        :label="$t(`reason.${item.label}`)"
        :value="item.value"
      />
    </el-radio-group>
    <div class="mt12 toolbar flex_aic_jcsb">
      <div class="addBtn flex_aic_jcc" @click="handAdd">
        <SvgIcon name="iconxinzeng" class="mr8" />
        <span>{{ $t('reason.新增原因') }}</span>
      </div>
    </div>
    <el-table
      :data="tableData"
      style="width: 100%"
      class="cover-table mt12"
      height="calc(100% - 240px)"
      border
      v-loading="loading"
    >
      <el-table-column :prop="'name' + locale" :label="$t('reason.原因标题')" />
      <el-table-column prop="type" :label="$t('reason.类型')">
        <template #default="scope">
          {{ options.find((item) => item.value === scope.row.type)?.label }}
        </template>
      </el-table-column>
      <el-table-column prop="createdTime" :label="$t('reason.创建时间')">
        <template #default="scope">
          {{ moment(scope.row.createdTime).format("YYYY-MM-DD HH:mm:ss") }}
        </template>
      </el-table-column>

      <el-table-column :label="$t('reason.操作')">
        <template #default="scope">
          <div class="flex_aic_jcc">
            <div class="operationBtn">
              <el-button type="text" size="small" @click="handleEdit(scope.row)"
                >{{ $t('reason.编辑') }}</el-button
              >
            </div>
            <el-popconfirm
              :title="$t('reason.确定删除吗？')"
              @confirm="handleDelete(scope.row)"
              :confirm-button-text="$t('reason.确定')"
              :cancel-button-text="$t('reason.取消')"
              width="260px"
            >
              <template #reference>
                <el-button class="ml0" type="text" size="small">{{ $t('reason.删除') }}</el-button>
              </template>
            </el-popconfirm>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <div class="flex justify-end mt24 absolute bottom-0 right-0">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[10, 20, 30, 40]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <EditDia
      v-model="editDiaVisible"
      :data="editDiaData"
      @success="getDataList"
    ></EditDia>
  </div>
</template>

<script setup>
import { ref, onMounted } from "vue";
import EditDia from "./editDia.vue";
import Api from "@/config/api/api";
import { ElMessage } from "element-plus";
import { useI18n } from "vue-i18n";
import moment from "moment";
import SearchCom from "@/components/SearchCom.vue";
import { useTableMixin } from "@/utils/tableMixin";
const { t } = useI18n();

const type = ref(1);
// 使用表格 mixin
const fetchMessages = (params) => {
  // 添加语言参数
  return Api.reasonList({
    ...params,
    type: type.value,
  });
};
const {
  currentPage,
  pageSize,
  total,
  tableData,
  loading,
  getDataList,
  handleSizeChange,
  handleCurrentChange,
  handleSearch,
  handleReset,
} = useTableMixin(fetchMessages);

const { locale } = useI18n();
const editDiaVisible = ref(false);
const editDiaData = ref({});

const options = [
  { label: t('reason.违规下架'), value: 1 },
  // { label: "商品审核拒绝", value: 2 },
  { label: t('reason.入驻审核拒绝'), value: 3 },
  // { label: t('reason.会员取+消订单'), value: 4 },
  { label: t('reason.仅退款'), value: 5 },
  { label: t('reason.退货退款'), value: 6 },
];
const searchList = [
  {
    label: t("reason.原因名称"),
    key: "name",
    type: "input",
  },
];

const toSearch = (params) => {
  handleSearch({ ["name" + locale.value]: params?.name });
};

const handleDelete = (row) => {
  Api.deleteReason([row.id]).then((res) => {
    const { status } = res;
    if (status === 200) {
      ElMessage.success(t("reason.删除成功"));
      currentPage.value = 1;
      getDataList();
    }
  });
};
const handleEdit = (row) => {
  editDiaVisible.value = true;
  editDiaData.value = JSON.parse(JSON.stringify(row));
};

const handAdd = () => {
  editDiaVisible.value = true;
  editDiaData.value = JSON.parse(JSON.stringify({ type: type.value }));
};
</script>

<style lang="scss" scoped>
.toolbar {
  width: 100%;
  height: 42px;
  margin-bottom: 5px;
  background: rgba(255, 126, 40, 0.1);
}
.addBtn {
  height: 28px;
  margin-left: 10px;
  padding: 0 7px;
  border-radius: 3px;
  background: #fff;
  cursor: pointer;
}
.split {
  width: 1px;
  height: 12px;
  background: #ddd;
  margin: 0 5px;
}
</style>
